package app.system;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.swing.filechooser.FileFilter;

public class KBFileFilter extends FileFilter{
    private String ext;
    private String[] multiext;
    public static Map<String, String> ds = new HashMap<String, String>();
    private static final String qboxExt = "qbox";
    
    static{
    	ds.put("all", "All acceptable files (*.kb/tbox/abox/txt)");
    	ds.put("kbox", "KB File (*.kbox)");
    	ds.put("txt", "Serialized File (*.txt)");
    	ds.put("tbox", "TBox File (*.tbox)");
    	ds.put("abox", "ABox File (*.abox)");
    	ds.put(qboxExt, "Query File (*.qbox)");
    }
    
    //if true, only qbox allowed. otherwise, all other file types.
    public KBFileFilter(boolean query){
        if(query){
        	this.multiext = null;
        	this.ext = qboxExt;
        }else{
        	String[] multiext2 = {"kb", "tbox", "txt", "abox"};	
        	this.multiext = multiext2;
        	this.ext = null;
        }
    }
//    public KBFileFilter(String[] mul){
//      this.multiext=mul;
//    }
    /**
     * This method overrides "accept", which accept directory and files owning
     * the intended extensions
     * @see class {@link javax.swing.filechooser.FileFilter}
     * @param file File
     * @return boolean
     */
    public boolean accept(File file){
      if (file.isDirectory()){
         return true;
      }
      String fileName=file.getName();
      String extension="";
      int index=fileName.lastIndexOf('.');
      if (index>0 && index<fileName.length()-1){
          //no file as xxx. or .xxx
          extension=fileName.substring(index+1).toLowerCase();
      }
      if(multiext!=null)
         for(int i=0;i<multiext.length;i++)
        	 if(multiext[i].equals(extension))
        	  return true;
      if(ext != null && extension.equals(ext))
        return true;
     return false;
    }
    
   public String getDescription(){
	   if(ext != null && ds.containsKey(ext)){
		   return ds.get(ext);
	   }else return ds.get("all");
    }
}

